<template>
	<view class="container">
		<view class="search-box">
			<input class="uni-input" v-model="text" confirm-type="search" placeholder="请输入搜索内容" @confirm="getCourseList" />
		</view>
		<view class="filter-box">
			<picker class="filter-item" @change="handleTypeChange" :value="currenType" :range="types">
				<view class="picker">
				  {{types[currentType]}}
				</view>
			</picker>
			<view :class="'filter-item ' + (sort === 2 ? 'active' : '')" @click="handleChangeSort(2)">销量</view>
			<view :class="'filter-item ' + (sort === 3 ? 'active' : '')" @click="handleChangeSort(3)">
				价格
				<image class="icon" src="../../static/sort.png"></image>
			</view>
		</view>
		<view class="list">
			<view class="item" v-for="(item, index) in list" :key="index">
				<media-item :options="item"></media-item>
			</view>
<!-- 			<view class="loading-more" v-if="isLoading || list.length > 8">
				<text class="loading-more-text">{{ tab.loadingText }}</text>
			</view> -->
		</view>
	</view>
</template>

<script>
	import mediaItem from './news-item.nvue'
	import { getCourse } from '@/api';
	import { getMarkCourseListByTitle } from '@/api';
	
	export default {
		components: {
			mediaItem
		},
		data() {
			return {
				text: '',
				list: [],
				types: ['综合', '考研', '考编', '英语四六级', '中小学课程'],
				currentType: 0,
				sort: 1
			}
		},
		methods: {
			// 获取课程数据
			getCourseList(type) {
				uni.showLoading({
					title: '搜索中...'
				})
				// return getCourse({type: this.sort, title: this.text || ''})
				return getMarkCourseListByTitle({type: this.sort, title: this.text || ''})
					.then(res => {
						let list = res.oData;
						console.log(type);
						if(type && !type.detail) {
							list = list.filter(item => item.tag === type);
						}
						if(this.sort === 2) {
							this.list = list.sort((a, b) => b.saleNums - a.saleNums);
						} else if(this.sort === 3) {
							this.list = list.sort((a, b) => a.realCost - b.realCost);
						} else {
							this.list = list;
						}
					})
					.finally(() => {
						uni.hideLoading();
					})
			},
			handleTypeChange(e) {
				if(this.currentType !== +e.detail.value) {
					this.currentType = +e.detail.value;
					this.getCourseList(this.types[+e.detail.value]);
				}
			},
			handleChangeSort(sort) {
				if(sort === this.sort) {
					this.sort = 1;
					return;
				} else {
					this.sort = sort;
				}
				this.getCourseList();
			}
		}
	}
</script>

<style>
	.filter-box {
		display: flex;
		background: #fff;
	}
	
	.icon {
		width: 10px;
		height: 10px;
	}
	
	.active {
		color: #007AFF;
	}
	
	.filter-item {
		flex: 1;
		font-size: 14px;
		padding: 5px 0;
		text-align: center;
	}
	
	.search-box {
		display: flex;
		justify-content: center;
		padding: 5px 0;
		margin-bottom: 5px;
		background-color: #fff;
	}
	
	.search-box .uni-input {
		width: 80%;
		height: 20px;
		padding: 5px 10px;
		line-height:30px;
		background-color: #F0F0F0;
		border-radius: 20px;
		font-size: 14px;
		text-align: center;
	}
	
	.list {
		display: flex;
		flex-wrap: wrap;
	}
	
	.item {
		width: 50%;
		margin: 0;
		padding: 0;
	}
</style>
